import { Component, OnDestroy } from '@angular/core';
import { Router, NavigationStart, NavigationEnd, NavigationCancel, NavigationError, RoutesRecognized } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { Object } from 'core-js/library/web/timers';

@Component({
  selector: 'leftmenu',
  templateUrl: './leftmenu.component.html',
  styleUrls: ['./leftmenu.component.css', '../../app.component.css']
})
export class LeftMenuComponent implements OnDestroy {
  ngOnDestroy(): void {
    if (this.routerlistener) {
      this.routerlistener.unsubscribe();
    }
  }
  routerlistener: Subscription;
  menus: Array<any> = [
    {
      id: 0,
      title: "资源监控",
      show: true,
      mainRouter: '/home/resource',
      src: '/assets/img/ziyuanguanli.png',
      srcSelect: '/assets/img/ziyuanguanli-select.jpg',
      srcActive: '/assets/img/ziyuanguanli-select.jpg',
      submenus: [
        {
          title: "物理机",
          router: 'psMachine',
        },
        {
          title: "云主机",
          router: 'cloudMachine',
        }
      ]
    },
    // {
    //   id: 1,
    //   title: "告警与监控",
    //   show: false,
    //   mainRouter: '/home/warn',
    //   src: '/assets/img/jiankong.jpg',
    //   srcSelect: '/assets/img/jiankong-select.png',
    //   srcActive: '/assets/img/jiankong-select2.png',
    //   submenus: [
    //     {
    //       title: "监控管理",
    //       router: 'watching/psmachine',
    //       matchRouter: ['watching/cloudmachine']
    //     },
    //     {
    //       title: "告警管理",
    //       router: 'warnning/notprocess',
    //       matchRouter: ['warnning/processed']  
    //     }
    //   ]
    // },
    {
      id: 2,
      title: "订单管理",
      show: false,
      src: '/assets/img/gongzuliu.jpg',
      srcSelect: '/assets/img/gongzuliu-select.png',
      srcActive: '/assets/img/gongzuliu-select2.png',
      mainRouter: '/home/workflow',
      submenus: [
        {
            title: "我的订单",
            router: 'order',
        },
        {
          title: "创建订单",
          router: 'order',
        },
        {
          title: "待审批",
          router: 'approval',
        },
        {
            title: "审批历史",
            router: 'approval',
        },
        {
          title: "工作流管理",
          router: 'procedure',
        }
      ]
    },
    {
        id: 3,
        title: "流程管理",
        show: false,
        src: '/assets/img/gongzuliu.jpg',
        srcSelect: '/assets/img/gongzuliu-select.png',
        srcActive: '/assets/img/gongzuliu-select2.png',
        mainRouter: '/home/workflow',
        submenus: [
          {
            title: "流程列表",
            router: 'procedure',
          },
          {
            title: "流程分配",
            router: 'procedureAssign',
          }
        ]
      }
  ]
  currentMenu: any;

  constructor(private router: Router) {

    
    this.routerlistener = router.events.subscribe(event => {
      if (event instanceof NavigationStart) {
        //不处理
      } else if (event instanceof NavigationEnd) {
        let url = event.url;
        let foundMenu, foundSubMenu;
        for (let menu of this.menus) {
          for (let submenu of menu.submenus) {
            if (menu.mainRouter + '/' + submenu.router == url) {
              foundMenu = menu;
              foundSubMenu = submenu;
            }
            if (foundMenu == null && foundSubMenu == null && submenu.matchRouter) {
              for (let r of submenu.matchRouter) {
                if (menu.mainRouter + '/' + r == url) {
                  foundMenu = menu;
                  foundSubMenu = submenu;                  
                }
              }
            }

            submenu.active = false;
          }
        }

        if (foundMenu && foundSubMenu) {
          foundMenu.show = true;
          foundSubMenu.active = true;  
          this.currentMenu = foundMenu.id;        
        }

      } else if (event instanceof NavigationCancel) {
        //不处理
      } else if (event instanceof NavigationError) {
        //不处理
      } else if (event instanceof RoutesRecognized) {
        //不处理
      }
    });
  }

  menuClick(menu) {
    menu.show = !menu.show;
  }

  subMenuClick(menu, submenu, id) {
    this.currentMenu = id;    
    this.router.navigate([menu.mainRouter + '/' + submenu.router]);
  }
}
